import bb.cascades 1.0
import "../chart/chartdata.js" as Chelp

Page {
    id: history_update
    
    property string history_mode
    property int fid
    property int amount
    property string time
    property int typeid
    property int index
    
    function setHistory(mode, id, am, ti, tid, inx, des){
        if (mode == "income"){
            ctn_income.show(tid);
            input_amount.text = am;
            input_des.text = des;
            history_mode = "income" ;
            fid = id;
            amount = am;
            time = ti;
            typeid = tid;
            index = inx;
        } else if (mode == "expenses"){
            ctn_expenses.show(tid);
            input_amount.text = am;
            input_des.text = des;
            history_mode = "expenses" ;
            fid = id;
            amount = am;
            time = ti;
            typeid = tid;
            index = inx;
        }
    }
    
    function update(){
        var input = Number(input_amount.text);
        if (input != NaN && input != "" && input != undefined && input > 0){
            if (history_mode == "income"){
                var rt = database.Update("t_bas_income", fid, {"famount": input, "fitypeid": dd_imcome_type.selectedValue, "FDESCRIPTION": input_des.text});
                if (rt){
                    root.showmsg(lang.history_update_success) ;
                    history_detail.update();
                    historyNavigation.pop();
                }else{
                    root.showmsg(lang.error_database) ;
                }
            } else if (history_mode == "expenses"){
                var rt = database.Update("t_bas_payment", fid, {"famount": input, "fptypeid": dd_expenses_type.selectedValue, "FDESCRIPTION": input_des.text});
                if (rt){
                    root.showmsg(lang.history_update_success) ;
                    history_detail.update();
                    historyNavigation.pop();
                }else{
                    root.showmsg(lang.error_database) ;
                }
            }
        
        }else {
            root.showmsg(lang.error_input) ;
        }
    }
    
    paneProperties: NavigationPaneProperties {
        backButton: ActionItem {
            onTriggered: {
                // define what happens when back button is pressed here
                // in this case is closed the detail page
                historyNavigation.pop()
            }
        }
    }
    
    actions: [
        // define the actions for first tab here
//        ActionItem {
//            title: qsTr("Rotate")
//            onTriggered: {
//                //                imgTab1.rotationZ = imgTab1.rotationZ + 90;
//            }
//        },
//        ActionItem {
//            title: qsTr("Break")
//            onTriggered: {
//                //                imgTab1.imageSource = "asset:///images/picture1br.png";
//            }
//        }
    ]
    
    titleBar: TitleBar {
        id: titleBar
        visibility: ChromeVisibility.Visible
        title: lang.history_title
    }
    
    Container {
        Container {
            layout: StackLayout {
                orientation: LayoutOrientation.LeftToRight
            }
            topPadding: 50
            Label {
                layoutProperties: StackLayoutProperties {
                    spaceQuota: 1
                }
                verticalAlignment: VerticalAlignment.Center
                text: lang.history_update_amount
            }
            TextField {
                layoutProperties: StackLayoutProperties {
                    spaceQuota: 3
                }
                id: input_amount
                inputMode: TextFieldInputMode.NumbersAndPunctuation
            }
        }
        Container {
            layout: StackLayout {
                orientation: LayoutOrientation.LeftToRight
            }
            topPadding: 50
            Label {
                layoutProperties: StackLayoutProperties {
                    spaceQuota: 1
                }
                text: lang.history_update_time
            }
            Label {
                layoutProperties: StackLayoutProperties {
                    spaceQuota: 3
                }
                text: history_update.time
            }
        }
        
        // type of income           
        Container {
            id: ctn_income
            layout: StackLayout {
                orientation: LayoutOrientation.LeftToRight
            }
            topPadding: 50
            maxHeight: 0
            
            function show(index)
                {
                                //                    dd_imcome_type.title = title;
                                dd_imcome_type.setSelectedIndex(index-1);
                                if (maxHeight == 0)
                maxHeight = 99999 ;
                }
                
                function hide()
                {
                                if (maxHeight > 0)
                maxHeight = 0 ;
                }
                
                Label {
                    layoutProperties: StackLayoutProperties {
                        spaceQuota: 1
                    }
                    verticalAlignment: VerticalAlignment.Center
                    text: lang.chart_income_type //chart_income_type or chart_expenses_type
                }
                
                DropDown {
                    id: dd_imcome_type
                    layoutProperties: StackLayoutProperties {
                        spaceQuota: 3
                    }
                    maxWidth: 600
                    
                    //                    selectedOption: option_income_all
                    //                    Option {
                    //                        id: option_income_all
                    //                        text : lang.type_all
                    //                        //                        description : "Sets the alarm to 2 minutes"
                    //                        value : ""
                    //                    
                    //                    }
                    Option {
                        id: option2
                        text : lang.income_salary
                        //                        description : "Sets the alarm to 1 minutes"
                        value : "1"
                    
                    }
                    
                    Option {
                        text : lang.income_investment
                        //                        description : "Sets the alarm to 2 minutes"
                        value : "2"
                    
                    }
                    Option {
                        text : lang.income_others
                        //                        description : "Sets the alarm to 3 minutes"
                        value : "3"
                    
                    }
                }
        }
        
        // type of expenses            
        Container {
            id: ctn_expenses
            layout: StackLayout {
                orientation: LayoutOrientation.LeftToRight
            }
            topPadding: 50
            maxHeight: 0
            
            function show(index){
                //                dd_expenses_type.title = title;
                dd_expenses_type.setSelectedIndex(index-1);
                if (maxHeight == 0)
maxHeight = 99999 ;
            }
            
            function hide(){
                if (maxHeight > 0)
maxHeight = 0 ;
            }
            
            Label {
                layoutProperties: StackLayoutProperties {
                    spaceQuota: 1
                }
                verticalAlignment: VerticalAlignment.Center
                text: lang.chart_expenses_type //chart_income_type or chart_expenses_type
            }
            
            DropDown {
                id: dd_expenses_type
                layoutProperties: StackLayoutProperties {
                    spaceQuota: 3
                }
                maxWidth: 600
                
                //                selectedOption: option_expense_all
                //                Option {
                //                    id: option_expense_all
                //                    text : lang.type_all
                //                    //                        description : "Sets the alarm to 2 minutes"
                //                    value : ""
                //                
                //                }
                Option {
                    id: option4
                    text : lang.expenses_food
                    //                        description : "Sets the alarm to 1 minutes"
                    value : "1"
                
                }
                
                Option {
                    text : lang.expenses_living
                    //                        description : "Sets the alarm to 2 minutes"
                    value : "2"
                
                }
                Option {
                    text : lang.expenses_entertainment
                    //                        description : "Sets the alarm to 3 minutes"
                    value : "3"
                
                }
                Option {
                    id: option7
                    text : lang.expenses_investment
                    //                        description : "Sets the alarm to 1 minutes"
                    value : "4"
                
                }
                
                Option {
                    text : lang.expenses_travel
                    //                        description : "Sets the alarm to 2 minutes"
                    value : "5"
                
                }
                Option {
                    text : lang.expenses_others
                    //                        description : "Sets the alarm to 3 minutes"
                    value : "6"
                
                }
            }
        }
        Container {
            topPadding: 50
            Label {
                text: lang.history_update_des
            }
            TextArea {
                id: input_des
            }
        }
        Container {
            topPadding: 50
            horizontalAlignment: HorizontalAlignment.Center
            Button {
                text: lang.history_update_button
                
                onClicked:{
                    history_update.update();
                }
            }
        }
    }
}
